using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Xml;
/// <summary>
/// General configuration of  <%= Config.GetUrlCaption() %>.
/// </summary>
public class Config
{
    private static UtilityBLL _utility = new UtilityBLL();

    public Config()
    { }

    #region General Settings
    /// <summary>
    /// Get Database connection string
    /// </summary>
    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.AppSettings["connectionString"].ToString();  // MySQL Compatible
            // return ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; // SQL SERVER Compatible
        }
    }

    /// <summary>
    /// Get current domain url
    /// </summary>
    public static string GetUrl()
    {
        string _url = ConfigurationManager.AppSettings["url"];
        if (_url.EndsWith("/") || _url.EndsWith(@"\"))
            return _url;
        else
            return _url + "/";
    }

    /// <summary>
    /// Get current domain url
    /// </summary>
    public static string GetUrl(string url)
    {
        string _url = ConfigurationManager.AppSettings["url"];
        if (_url.EndsWith("/") || _url.EndsWith(@"\"))
            return _url + "" + url;
        else
            return _url + "/" + url;
    }

    ///// <summary>
    ///// Prepare DateDiff for SQL SERVER / MYSQL
    ///// </summary>
    //public static string Prepare_DateDiff(string field, int type)
    //{
    //    string value = "";
    //    // type 0: today, 1: this week, 2: this month
    //    int servertype = 1;
    //    // servertype 0: sql server 2005, 1: mysql, 2: sql server 2000
    //    switch (servertype)
    //    {
    //        case 0:
    //            value = Prepare_DateDiff_SQLSERVER(field, type);
    //            break;
    //        case 1:
    //            value = Prepare_DateDiff_MYSQL(field, type);
    //            break;
    //        case 2:
    //            value = Prepare_DateDiff_SQLSERVER(field, type);
    //            break;
    //    }
    //    return value;
    //}
    ///// <summary>
    ///// Prepare DateDiff for SQL SERVER
    ///// </summary>
    //private static string Prepare_DateDiff_SQLSERVER(string field, int type)
    //{
    //    string value = "";
    //    switch (type)
    //    {
    //        case 0:
    //            // today
    //            value = "DateDiff(DAY," + field + ",getdate())=0";
    //            break;
    //        case 1:
    //            // this week 
    //            value = "DateDiff(DAY," + field + ",getdate())>=0 AND DateDiff(DAY," + field + ",getdate())<7";
    //            break;
    //        case 2:
    //            // this month
    //            value = "DateDiff(DAY," + field + ",getdate())>=0 AND DateDiff(DAY," + field + ",getdate())<31";
    //            break;
    //        case 3:
    //            // all records within this week
    //            value = "DateDiff(DAY," + field + ",getdate())<7";
    //            break;
    //        case 4:
    //            // all records within this month
    //            value = "DateDiff(DAY," + field + ",getdate())<31";
    //            break;
    //    }
    //    return value;
    //}

    ///// <summary>
    ///// Prepare DateDiff for MYSQL
    ///// </summary>
    //private static string Prepare_DateDiff_MYSQL(string field, int type)
    //{
    //    string value = "";
    //    switch (type)
    //    {
    //        case 0:
    //            // today
    //            value = "DateDiff(" + field + ",CURDATE())=0";
    //            break;
    //        case 1:
    //            // this week 
    //            value = "DateDiff(" + field + ",CURDATE())<=0 AND DateDiff(" + field + ",CURDATE())>-7";
    //            break;
    //        case 2:
    //            // this month
    //            value = "DateDiff(" + field + ",CURDATE())<=0 AND DateDiff(" + field + ",CURDATE())>-31";
    //            break;
    //        case 3:
    //            // all records within this week
    //            value = "DateDiff(" + field + ",CURDATE())>-7";
    //            break;
    //        case 4:
    //            // all records within this month
    //            value = "DateDiff(" + field + ",CURDATE())>-31";
    //            break;
    //    }
    //    return value;
    //}

    /// <summary>
    /// Validate administrator readonly status.
    /// </summary>
    public static bool isAdminActionAllowed()
    {
        if (HttpContext.Current.Session["AccessLevel"] != null)
        {
            if (HttpContext.Current.Session["AccessLevel"].ToString() == "Full")
                return true;
            else
                return false;
        }
        else
        {
            return false;
        }

    }

    /// <summary>
    /// Return current page url and append querystring
    /// </summary>
    public static string Return_Current_Page(string value)
    {
        string prot = "http";
        string https = HttpContext.Current.Request.ServerVariables["HTTPS"];
        if (https != "off")
            prot = "https";

        string domainname = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
        string filename = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
        //string querystring = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
        return prot + "://" + domainname + filename + "?" + value;
    }

    /// <summary>
    /// Return current page url.
    /// </summary>
    public static string Return_Current_Page()
    {
        string prot = "http";
        string https = HttpContext.Current.Request.ServerVariables["HTTPS"];
        if (https != "off")
            prot = "https";

        string domainname = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
        string filename = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"];
        //string querystring = HttpContext.Current.Request.ServerVariables["QUERY_STRING"];
        //if (querystring != "")
        //    return prot + "://" + domainname + filename + "?" + querystring;
        //else
        return prot + "://" + domainname + filename;
    }
    #endregion

  
  

    #region Message Layouts




    /// <summary>
    /// Show message panel on page and display information.
    /// </summary>
    public static void ShowMessage(System.Web.UI.HtmlControls.HtmlGenericControl pnl, string message, int messagetype, int icontype)
    {
        //messagetype :-> 0: Error, 1:Hightlited
        // icontype :-> 0: Alert, 1:Info, 2:Success
        string icon_class = "ui-icon-alert";
        string icon_message = "Alert";
        string message_class = "ui-state-error";
        switch (messagetype)
        {
            case 0:
                message_class = "ui-state-error";
                break;
            case 1:
                message_class = "ui-state-highlight";
                break;
        }
        switch (icontype)
        {
            case 0:
                icon_class = "ui-icon-alert";
                icon_message = "Alert:";
                break;
            case 1:
                icon_class = "ui-icon-info";
                icon_message = "Info:";
                break;
            case 2:
                icon_class = "ui-icon-check";
                icon_message = "Success:";
                break;
        }
        StringBuilder msg = new StringBuilder();
        msg.Append("<div class=\"item_pad_2\"><div class=\"" + message_class + " ui-corner-all\">");
        msg.Append("<p><span class=\"ui-icon " + icon_class + "\" style=\"float: left; margin-right: .3em;\"></span>");
        msg.Append("<strong>" + icon_message + "</strong> ");
        msg.Append(message);
        msg.Append("</p></div></div>");

        pnl.InnerHtml = msg.ToString();
    }

    /// <summary>
    /// Show message panel on page and display information.
    /// </summary>
    public static void ShowMessage(System.Web.UI.HtmlControls.HtmlGenericControl pnl, string message, int messagetype, int icontype, int width)
    {
        //messagetype :-> 0: Error, 1:Hightlited
        // icontype :-> 0: Alert, 1:Info, 2:Success
        string icon_class = "ui-icon-alert";
        string icon_message = "Alert";
        string message_class = "ui-state-error";
        switch (messagetype)
        {
            case 0:
                message_class = "ui-state-error";
                break;
            case 1:
                message_class = "ui-state-highlight";
                break;
        }
        switch (icontype)
        {
            case 0:
                icon_class = "ui-icon-alert";
                icon_message = "Alert:";
                break;
            case 1:
                icon_class = "ui-icon-info";
                icon_message = "Info:";
                break;
            case 2:
                icon_class = "ui-icon-check";
                icon_message = "Success:";
                break;
        }
        StringBuilder msg = new StringBuilder();
        msg.Append("<div class=\"" + message_class + " ui-corner-all\" style=\"width:" + width + "px; margin:4px auto 4px auto;\">");
        msg.Append("<p><span class=\"ui-icon " + icon_class + "\" style=\"float: left; margin-right: .3em;\"></span>");
        msg.Append("<strong>" + icon_message + "</strong> ");
        msg.Append(message);
        msg.Append("</p></div>");

        pnl.InnerHtml = msg.ToString();
    }

    /// <summary>
    /// Show message panel on page and display information.
    /// </summary>
    public static string ShowMessage(string message, int messagetype, int icontype, int width)
    {
        //messagetype :-> 0: Error, 1:Hightlited
        // icontype :-> 0: Alert, 1:Info, 2:Success
        string icon_class = "ui-icon-alert";
        string icon_message = "Alert";
        string message_class = "ui-state-error";
        switch (messagetype)
        {
            case 0:
                message_class = "ui-state-error";
                break;
            case 1:
                message_class = "ui-state-highlight";
                break;
        }
        switch (icontype)
        {
            case 0:
                icon_class = "ui-icon-alert";
                icon_message = "Alert:";
                break;
            case 1:
                icon_class = "ui-icon-info";
                icon_message = "Info:";
                break;
            case 2:
                icon_class = "ui-icon-check";
                icon_message = "Success:";
                break;
        }
        StringBuilder msg = new StringBuilder();
        msg.Append("<div class=\"" + message_class + " ui-corner-all\" style=\"width:" + width + "px; margin:4px auto 4px auto;\">");
        msg.Append("<p><span class=\"ui-icon " + icon_class + "\" style=\"float: left; margin-right: .3em;\"></span>");
        msg.Append("<strong>" + icon_message + "</strong> ");
        msg.Append(message);
        msg.Append("</p></div>");

        return msg.ToString();
    }

    /// <summary>
    /// Show message panel on page and display information. / animate / width options
    /// </summary>
    public static void ShowMessage(System.Web.UI.HtmlControls.HtmlGenericControl pnl, string message, int messagetype, int icontype, int width, string clientid)
    {
        //messagetype :-> 0: Error, 1:Hightlited
        // icontype :-> 0: Alert, 1:Info, 2:Success
        string icon_class = "ui-icon-alert";
        string icon_message = "Alert";
        string message_class = "ui-state-error";
        switch (messagetype)
        {
            case 0:
                message_class = "ui-state-error";
                break;
            case 1:
                message_class = "ui-state-highlight";
                break;
        }
        switch (icontype)
        {
            case 0:
                icon_class = "ui-icon-alert";
                icon_message = "Alert:";
                break;
            case 1:
                icon_class = "ui-icon-info";
                icon_message = "Info:";
                break;
            case 2:
                icon_class = "ui-icon-check";
                icon_message = "";
                break;
        }
        StringBuilder msg = new StringBuilder();
        msg.Append("<script type=\"text/javascript\">");
        msg.Append("$(document).ready(function(){");
        msg.Append("$('#" + clientid + "').fadeOut(10000);");
        msg.Append("});</script>");
        msg.Append("<div class=\"" + message_class + " ui-corner-all\" style=\"width:" + width + "px; margin:4px auto 4px auto;\">");
        msg.Append("<p><span class=\"ui-icon " + icon_class + "\" style=\"float: left; margin-right: .3em;\"></span>");
        msg.Append("<strong>" + icon_message + "</strong> ");
        msg.Append(message);
        msg.Append("</p></div>");

        pnl.InnerHtml = msg.ToString();
    }
    
    /// <summary>
    /// Show message panel on page and display information.
    /// </summary>
    public static void ClearMessage(System.Web.UI.HtmlControls.HtmlGenericControl pnl)
    {
        pnl.InnerHtml = "";
    }

    #endregion
}
